﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.Controls
{
    [Serializable]
    public partial class VouchertListCtrl : BaseListControl, IListControl
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        public event ProductNameClickEventHandler ProductNameClick;

        protected void OnProductNameClick(EventArgs e)
        {
            if (ProductNameClick != null)
                ProductNameClick(this, e);
        }

        public Vouchers ProductList
        {
            get { return (Vouchers)SourceObject; }
            set { SourceObject = value; }
        }

        public String CategoryListSessionKey
        {
            get 
            {
                if(ViewState[ClientID + "_CategoryListSessionKey"] == null)
                    ViewState[ClientID + "_CategoryListSessionKey"] = Guid.NewGuid().ToString() + "_" + ClientID + "_category";
                return ViewState[ClientID + "_CategoryListSessionKey"].ToString();
            }
            set { ViewState[ClientID + "_CategoryListSessionKey"] = value; }
        }

        protected void lnkProductName_OnClick(object sender, EventArgs e)
        {
            OnProductNameClick(e);
        }

        protected override void grid_OnItemDataBound(object sender, DataGridItemEventArgs e)
        {
        }

        #region IListControl Members

        public void DeleteCheckedItems()
        {
            String checkedItemID = "";
            for (int i = 0; i < grid.Items.Count; i++)
            {
                DataGridItem item = grid.Items[i];
                Control ctrl = item.FindControl("cbSelect");
                if (ctrl != null && ctrl is CheckBox && ((CheckBox)ctrl).Checked && ((CheckBox)ctrl).Attributes["ItemID"] != null)
                    checkedItemID += "'" + ((CheckBox)ctrl).Attributes["ItemID"].ToString() + "',";
            }
            if (checkedItemID != "")
            {
                checkedItemID = checkedItemID.Trim(',');
                Vouchers.DeleteByIds(checkedItemID);
            }
        }

        protected void btnActive_OnClick(object sender, EventArgs e)
        {
            Guid id = new Guid(hidActive.Value);
            Vouchers.SetActive(id);
            Refresh();
        }

        public void DeleteSelectedItem()
        {
            throw new NotImplementedException();
        }

        public void AddNewItem(MyGeneration.dOOdads.SqlClientEntity newItem)
        {
            throw new NotImplementedException();
        }

        public void UpdateItem(MyGeneration.dOOdads.SqlClientEntity updatedItem)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}